package com.music.mapper.music;

import com.music.dto.music.DownloadDTO;
import com.music.entity.music.DownloadEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface DownloadMapper {
    /*
     * @Description 查询下载
     * @Author hbh
     * @Date 2021/12/17
     **/
    @Select("<script>SELECT download_id as 'key', download_id, a.song_id, download_status, a.gmt_create, " +
            "song_name, singer_name, album_name \n" +
            "from music_download a LEFT JOIN music_song b ON a.song_id = b.song_id\n" +
            "WHERE a.user_id = #{userId}\n" +
            "<if test='songName != null'> AND song_name LIKE CONCAT('%', #{songName}, '%') </if>" +
            "<if test='downloadStatus != null'> AND download_status = #{downloadStatus} </if>" +
            "and is_delete = 0 " +
            "ORDER BY " +
            "<if test='sortField != null'>" +
            "${sortField} ${sortOrder}, " +
            "</if>" +
            "download_id desc " +
            "LIMIT #{start}, #{length}" +
            "</script>")
    List<DownloadDTO> list4Table(@Param("start") Integer start,
                                 @Param("length") Integer length,
                                 @Param("userId") Long userId,
                                 @Param("songName") String songName,
                                 @Param("downloadStatus") Integer downloadStatus,
                                 @Param("sortField") String sortField,
                                 @Param("sortOrder") String sortOrder) throws Exception;

    /*
     * @Description 查询下载数量
     * @Author hbh
     * @Date 2021/12/17
     **/
    @Select({"<script>SELECT COUNT(*) FROM music_download a left join music_song b on a.song_id = b.song_id " +
            "WHERE user_id = #{userId} " +
            "and is_delete = 0 " +
            "<if test='songName != null'> AND song_name LIKE CONCAT('%', #{songName}, '%') </if>" +
            "<if test='downloadStatus != null'> AND download_status = #{downloadStatus} </if>" +
            "</script>"})
    Long count4Table(@Param("userId") Long userId,@Param("songName") String songName,
                     @Param("downloadStatus") Integer downloadStatus) throws Exception;
    /*
     * @Description 新增下载
     * @Author hbh
     * @Date 2021/12/17
     **/
    @Insert("Insert into music_download ( " +
            "user_id, song_id, download_status, gmt_create, is_delete) " +
            "VALUES (#{downloadEntity.userId}, #{downloadEntity.songId}, 3, " +
            "#{downloadEntity.gmtCreate}, 0)")
    @Options(useGeneratedKeys = true, keyProperty = "downloadId", keyColumn = "download_id")
    Integer add(@Param("downloadEntity") DownloadEntity downloadEntity) throws Exception;

    /*
     * @Description 删除下载记录
     * @Author hbh
     * @Date 2021/12/17
     **/
    @Delete("update music_download set is_delete = 1 where download_id = #{downloadId}")
    Integer remove(@Param("downloadId") Long downloadId) throws Exception;

    /*
     * @Description 修改下载记录
     * @Author hbh
     * @Date 2021/12/17
     **/
    @Update("update music_download set download_status = #{downloadStatus} where download_id = #{downloadId}")
    Integer update(@Param("downloadId") Long downloadId,
                   @Param("downloadStatus")Integer downloadStatus) throws Exception;

    /*
     * @Description 判断下载失败记录
     * @Author hbh
     * @Date 2021/12/19
     **/
    @Update("UPDATE music_download set download_status = 4 \n" +
            "WHERE download_status = 3 AND TIMESTAMPDIFF(MiNUTE,gmt_create,now()) > 0")
    Integer judgeFailed() throws Exception;
}
